package cn.zebra.dev.common.restful;

/**
 * 返回数据包装类
 *
 * @author Junit@vip.qq.com
 * @date 2019年8月27日 22:42:57
 */
public final class Result<T> {
    /**
     * 成功
     */
    public static final Integer SUCCESS = 200;

    /**
     * 返回状态
     */
    private Integer status = SUCCESS;
    /**
     * 返回信息
     */
    private String message = "success";
    /**
     * 返回数据
     */
    private T data;

    private Long timestamp = System.currentTimeMillis();

    protected Result() {
        this.message = "success";
    }

    protected Result(Integer status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public Result(T data) {
        this.setData(data);
    }

    /**
     * 返回错误信息
     *
     * @return 错误信息
     */
    public static <T> Result<T> error() {
        Result<T> result = new Result<>();
        result.setStatus(500);
        result.setMessage("error");
        return result;
    }

    /**
     * 返回错误信息
     *
     * @param message 错误信息
     * @return 错误信息
     */
    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setStatus(500);
        result.setMessage(message);
        return result;
    }

    /**
     * 返回成功信息
     *
     * @param message 成功信息
     * @return 成功信息
     */
    public static Result success(String message) {
        Result result = new Result();
        result.setMessage(message);
        return result;
    }

    /**
     * 返回成功信息
     *
     * @param data 成功信息
     * @return 成功信息
     */
    public static Result data(String data) {
        Result<String> result = new Result<>();
        result.setData(data);
        return result;
    }

    public static Result success() {
        return new Result();
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        return result;
    }

    public static Result error(Integer status, String message) {
        Result result = new Result();
        result.setStatus(status);
        result.setMessage(message);
        return result;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public String getMessage() {
        return message;
    }

    public Integer getStatus() {
        return status;
    }

    @Override
    public String toString() {
        return "{" +
                "status=" + status +
                ", message='" + message + '\'' +
                ", data=" + data +
                ", timestamp=" + timestamp +
                '}';
    }
}
